import { _decorator, Button, Component, Label, Node, sys } from 'cc';
import EventExt from 'db://cocos-ext/base/EventExt';
import { UtilCocos } from 'db://cocos-ext/base/UtilCocos';

import YYExt from '../../yyExt/YYExt';
import SlotBetModule from './SlotBetModule';
import {
    SLOT_AUTO_SPIN_NEXT, SLOT_AUTO_SPIN_START, SLOT_AUTO_SPIN_STOP, SLOT_SHOW_AUTO_BET_PANEL,
    SLOT_SPIN_BTN_ENABLE, SLOT_SPIN_STOP, SLOT_STOP_BTN_ENABLE
} from './SlotEventType';

const { ccclass, property } = _decorator;

@ccclass('SlotSpinBtns')
export default class SlotSpinBtns extends Component {

    @property(Node)
    private readonly btnStart: Node = null;

    @property(Node)
    private readonly btnStop: Node = null;

    @property(Node)
    private readonly autoStopBtn: Node = null;
    @property(Label)
    private readonly autoCountLabel: Label = null;

    private isSpinEnable: boolean = true;
    private _touchStartTime: number = 0;

    protected onLoad(): void {
        this.btnStart.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.btnStart.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.btnStart.on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);

        this.autoStopBtn.active = false;
        this.autoStopBtn.on(Button.EventType.CLICK, this.onStopAutoBtnClick, this);
        this.btnStop.active = false;
        this.btnStop.on(Button.EventType.CLICK, this.onStopBtnClick, this);

        // custom event
        EventExt.on(SLOT_AUTO_SPIN_START, this.startAuto, this);
        EventExt.on(SLOT_AUTO_SPIN_STOP, this.stopAutoBet, this);
        EventExt.on(SLOT_AUTO_SPIN_NEXT, this.nextAutoBet, this);
        // 转盘开始按钮是否启用
        EventExt.on(SLOT_SPIN_BTN_ENABLE, (v: boolean) => {
            this.isSpinEnable = v;
            UtilCocos.setNodeGray(this.btnStart, !v);
        }, this);
        // 停止按钮是否显示事件
        EventExt.on(SLOT_STOP_BTN_ENABLE, (bol: boolean) => {
            this.btnStop.active = bol;
        }, this);
    }

    protected onDestroy(): void {
        this.btnStart.off(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.btnStart.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.btnStart.off(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
        EventExt.targetOff(this);
    }

    /** next round */
    private nextAutoBet() {
        this.count--;
        if (this.count > -1) {
            SlotBetModule.spinStart();
        }
    }

    /** stop auto bet */
    private stopAutoBet() {
        this.count = -1;
    }

    /** start auto bet */
    private startAuto(autoCnt: number) {
        if (SlotBetModule.betValue <= 0)
            return;
        const coinCheckCnt = autoCnt > 1000 ? 1 : autoCnt;
        if (!YYExt.checkMoney(coinCheckCnt * SlotBetModule.betValue)) {
            return;
        }
        this.count = autoCnt;
        this.nextAutoBet();
    }

    /** 剩余自动旋转次数 */
    private get count(): number {
        return SlotBetModule.autoBetCnt;
    }
    private set count(val: number) {
        SlotBetModule.autoBetCnt = val;
        // update label
        this.autoCountLabel.string = val.toString();
        this.autoStopBtn.active = val > 0;
    }

    /** on betton clicked stop */
    private onStopAutoBtnClick() {
        this.stopAutoBet();
    }

    /** on button clicked stop */
    private onStopBtnClick() {
        this.btnStop.active = false;
        this._touchStartTime = 0;
        EventExt.emit(SLOT_SPIN_STOP);
    }

    private onTouchStart() {
        this.unschedule(this.dealTouch);
        if (!this.isSpinEnable || this.btnStop.active) {
            this._touchStartTime = 0;
            return;
        }
        this._touchStartTime = sys.now();
        this.scheduleOnce(this.dealTouch, 1);
    }

    private onTouchEnd() {
        if (!this.isSpinEnable) {
            this._touchStartTime = 0;
            return;
        }
        this.dealTouch();
    }

    private onTouchCancel() {
        this._touchStartTime = 0;
        this.unschedule(this.dealTouch);
    }

    private dealTouch() {
        this.unschedule(this.dealTouch);
        if (this._touchStartTime == 0) return;
        const duration = sys.now() - this._touchStartTime;
        this._touchStartTime = 0
        if (duration > 1 * 1000) {
            EventExt.emit(SLOT_SHOW_AUTO_BET_PANEL);
        } else {
            SlotBetModule.spinStart();
        }
    }

}
